﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IntfClass;
using SunLightIntfService;
using System.Data.SqlClient;

namespace IntfSetting.FrmIntf
{
    public partial class FrmIntfSetting : Form
    {
        public FrmIntfSetting(string Code)
        {
            InitializeComponent();


            //因为窗体上看不到控件，不能添加，所以动态添加这两个控件
            lablel15.Location = new Point(10, 140);//第一个代表横坐标。第2个代表纵坐标。
            lablel15.Text = "每次文件下载数";
            lablel15.Size = new System.Drawing.Size(100, 12);

            txtDownNum.Location = new Point(110, 135);
            txtDownNum.Size = new System.Drawing.Size(60, 12);
            txtDownNum.Text = "5";
            this.txtDownNum.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtThreadNum_KeyPress);
            

            this.Controls.Add(lablel15);
            this.grpFrequency.Controls.Add(lablel15);

            this.Controls.Add(txtDownNum);
            this.grpFrequency.Controls.Add(txtDownNum);


            if (!Code.Equals(string.Empty))
            {
                strCode = Code;
                blnNew = false;

            }
        }

        private Boolean blnNew = true;
        private String strCode = string.Empty;
        private DataTable dtRule = new DataTable();
        private DataTable dtUpRulelist = new DataTable();
        private DataTable dtDownRulelist = new DataTable();
        private DataRow drUpRule;
        private DataRow drDownRule;

        //因为窗体上看不到控件，不能添加，所以动态添加这两个控件
        private Label lablel15 = new Label();
        private TextBox txtDownNum = new TextBox();



        private string strUpStep = string.Empty;
        private string strDownStep = string.Empty;
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtThreadNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            SetRule(true,drUpRule);
        }

        private void SetRule(Boolean blnUp,DataRow drRow)
        {

            FrmFreStep1 frmFreStep1 = new FrmFreStep1(drRow);
            if (frmFreStep1.ShowDialog() == DialogResult.OK)
            {
                if (blnUp)
                {
                    dtUpRulelist = frmFreStep1.dtStartTime;

                }
                else
                {
                    dtDownRulelist = frmFreStep1.dtStartTime;
                }
                //drRows[0]["Frequency"] = frmFreStep1.str
                //string str1 = "sssssss";
                frmFreStep1.Dispose();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (drDownRule == null)
            {
               
                drDownRule = dtRule.NewRow();
                //drDownRule["ID"] = drUpRule["ID"];
                drDownRule["ParentCode"] = drUpRule["ParentCode"];
                drDownRule["Kind"] = drUpRule["Kind"];
                drDownRule["blnUp"] = 0;
                dtRule.Rows.Add(drDownRule);

            }

            SetRule(false,drDownRule);
        }

        private void FrmIntfSetting_Load(object sender, EventArgs e)
        {
            this.comDataClass.SelectedIndex = 0;
            InitializeCon();
            dtRule = SearchRule(strCode);

            if (!blnNew)
            {
                DataTable dtData = new DataTable();
                dtData = SearchSetting(strCode);

                this.txtCode.ReadOnly = true;
                SetFormView(dtData);

                drUpRule = dtRule.Select("blnUp = 1")[0];
                dtUpRulelist = SearchRuleList(drUpRule["ID"].ToString());

                if (dtRule.Select("blnUp = 0").Length > 0)
                {
                    drDownRule = dtRule.Select("blnUp = 0")[0];
                    dtDownRulelist = SearchRuleList(drDownRule["ID"].ToString());

                }

                this.comDataClass.SelectedIndex = Convert.ToInt16(dtData.Rows[0]["DataClass"].ToString());

                this.comSend.SelectedValue = dtData.Rows[0]["Uper"].ToString();
                this.comReceive.SelectedValue = dtData.Rows[0]["Downer"].ToString();

                //dtUpRulelist = SearchStartTime();
            }
            else
            {
                drUpRule = dtRule.NewRow();
                drUpRule["blnUp"] = 1;
                dtRule.Rows.Add(drUpRule);

                drDownRule = dtRule.NewRow();
                drDownRule["blnUp"] = 0;
                dtRule.Rows.Add(drDownRule);

            }
        }

        private void InitializeCon()
        {
            InitializeSideCon();

            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("select t.ID,t.code,t.name from TBusiness t");

            DataTable dtBusinessData = new DataTable();
            ClassDB classDB = new ClassDB();
            dtBusinessData = classDB.ExecuteDataTable(strSql.ToString());

            this.comBusiness.DataSource = dtBusinessData;
            this.comBusiness.ValueMember = "Code";
            this.comBusiness.DisplayMember = "Name";



        }

        private void InitializeSideCon()
        {
            StringBuilder strSql = new StringBuilder();
            if (radClass1.Checked)
            {
                strSql.AppendLine("select t.ID,t.code,t.name from TSide t where t.code <> 'DMS'");
            }
            else
            {
                strSql.AppendLine("select t.ID,t.code,t.name from TSide t where (t.code = 'DCS' OR t.code = 'DMS')");
            }

            DataTable dtSideData = new DataTable();
            ClassDB classDB = new ClassDB();
            dtSideData = classDB.ExecuteDataTable(strSql.ToString());

            if (!radClass1.Checked)
            {
                if (dtSideData.Rows.Count == 0)
                {
                    MessageBox.Show("DCS或DMS未设置！");
                }
                this.comDataClass.Visible = true;
                if (this.comDataClass.SelectedIndex == 0)
                {
                    this.radOn.Enabled = false;
                }

            }
            else
            {
                this.comDataClass.Visible = false;
                this.radOn.Enabled = true;


            }

            this.comReceive.DataSource = dtSideData;
            this.comReceive.ValueMember = "Code";
            this.comReceive.DisplayMember = "Name";
            this.comReceive.Text = "";
            this.comSend.DataSource = dtSideData.Copy();
            this.comSend.ValueMember = "Code";
            this.comSend.DisplayMember = "Name";
            this.comSend.Text = "";

        }

        private DataTable SearchSetting(string Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("Select t.* from TIntf t");
            strSql.AppendLine("Where t.Code = '" + Code + "'");

            DataTable dtData = new DataTable();
            ClassDB classDB = new ClassDB();
            dtData = classDB.ExecuteDataTable(strSql.ToString());

            return dtData;
        }

        private void SetFormView(DataTable dtData)
        {
            this.txtCode.Text = dtData.Rows[0]["Code"].ToString();
            this.txtName.Text = dtData.Rows[0]["Name"].ToString();
            this.txtMemo.Text = dtData.Rows[0]["Memo"].ToString();

            this.comBusiness.SelectedValue = dtData.Rows[0]["BusinessCode"].ToString();
            //this.comSend.SelectedValue = dtData.Rows[0]["Uper"].ToString();
            //this.comReceive.SelectedValue = dtData.Rows[0]["Downer"].ToString();

            this.txtSendFun.Text = dtData.Rows[0]["UpFunction"].ToString();
            this.txtReceiveFun.Text = dtData.Rows[0]["DownFunction"].ToString();

            if (dtData.Rows[0]["blnStep"].ToString().Equals("1"))
            {
                radDelay.Checked = true;
            }
            else
            {
                radOn.Checked = true;   
            }

            this.txtThreadNum.Text = dtData.Rows[0]["ThreadNum"].ToString();
            this.txtNum.Text = dtData.Rows[0]["MaxNum"].ToString();

            //新加文件下载数
            this.txtDownNum.Text = dtData.Rows[0]["DownloadCapacity"].ToString();

            if (dtData.Rows[0]["Class"].ToString().Equals("1"))
            {
                radClass1.Checked = true;
            }
            else
            {
                radClass0.Checked = true;
            }
        }



        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private Boolean CheckData()
        {
            try
            {
                Boolean blnReturn = true;
                if (txtCode.Text.Trim().Equals(string.Empty))
                {
                    MessageBox.Show("编号不可为空");
                    txtCode.Focus();
                    return false;
                }

                if (txtName.Text.Trim().Equals(string.Empty))
                {
                    MessageBox.Show("名称不可为空");
                    txtName.Focus();
                    return false;
                }

                if (comBusiness.Text.ToString().Equals(string.Empty))
                {
                    MessageBox.Show("业务不可为空");
                    comBusiness.Focus();
                    return false;
                }

                if (comSend.Text.ToString().Equals(string.Empty))
                {
                    MessageBox.Show("发送方不可为空");
                    comSend.Focus();
                    return false;
                }

                if (txtSendFun.Text.Trim().Equals(string.Empty))
                {
                    MessageBox.Show("发送方方法不可为空");
                    txtSendFun.Focus();
                    return false;
                }

                if (comReceive.Text.ToString().Equals(string.Empty))
                {
                    MessageBox.Show("接收方不可为空");
                    comReceive.Focus();
                    return false;
                }
                
                if (txtReceiveFun.Text.Trim().Equals(string.Empty))
                {
                    MessageBox.Show("接收方方法不可为空");
                    txtReceiveFun.Focus();
                    return false;
                }

                if (txtThreadNum.Text.Equals(string.Empty) || Convert.ToInt16(txtThreadNum.Text) < 1|| Convert.ToInt16(txtThreadNum.Text) > 5)
                {
                    MessageBox.Show("线程数必须大于1小于5");
                    txtThreadNum.Focus();
                    return false;
                }

                if (txtNum.Text.Equals(string.Empty) || Convert.ToInt16(txtNum.Text) < 0 )
                {
                    MessageBox.Show("一次同步数数不能小于0");
                    txtNum.Focus();
                    return false;
                }

                if(txtDownNum.Text.Equals(string.Empty) || Convert.ToInt16(txtDownNum.Text) < 0) {
                    MessageBox.Show("每次文件下载数不能小于0");
                    txtDownNum.Focus();
                    return false;
                }

                DataRow[] drRows = dtRule.Select("blnUp = 1");
                if (drRows.Length == 0)
                {
                    MessageBox.Show("上传规则未设定");
                    btnSend.Focus();
                    return false;
                }

                if (radDelay.Checked)
                {
                    drRows = dtRule.Select("blnUp = 0");
                    if (drRows.Length == 0)
                    {
                        MessageBox.Show("接收规则未设定");
                        btnSend.Focus();
                        return false;
                    }
                }

                return blnReturn;

            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            
            //DataRow[] drUpRows = dtRule.Select("blnUp = 1");
            //intfsetting intfsetting = new intfsetting();
            if (radStatr1.Checked)
            {
                this.CacuStartTime(drUpRule["Kind"].ToString(), drUpRule, dtUpRulelist);

                if (radDelay.Checked)
                {
                    this.CacuStartTime(drDownRule["Kind"].ToString(), drDownRule, dtDownRulelist);
                }

            }
            else
            {
                drUpRule["ExecutionTime"] = DateTime.Now;
                drDownRule["ExecutionTime"] = DateTime.Now;

            }

            if (blnNew)
            {
                //新增
                if(ExcueNew() == 1) {

                    return;
                }
                
            }
            else
            {
                //修改
                ExcueUpdate();
            }

            this.Dispose();


        }

        private void ExcueUpdate()
        {
            StringBuilder strSql = new StringBuilder();


            ClassDB classDB = new ClassDB();

            SqlParameter[] sqlParameters = this.CreateTIntfParameters();
            SqlConnection conn = classDB.CreateConnection();
            conn.Open();
            SqlTransaction sqlTransaction = conn.BeginTransaction();
            try
            {

                strSql.AppendLine("Delete from TRulelist Where ParentID = " + drUpRule["ID"].ToString());
                strSql.AppendLine(";");
                if (drDownRule != null && !(string.IsNullOrEmpty(drDownRule["ID"].ToString())))
                {
                    strSql.AppendLine("Delete from TRulelist Where ParentID = " + drDownRule["ID"].ToString());
                    strSql.AppendLine(";");
                }
                classDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, sqlParameters);

                strSql = new StringBuilder();
                strSql.AppendLine("Update TIntf Set");
                strSql.AppendLine("Name = @Name");
                strSql.AppendLine(",BusinessCode = @BusinessCode");
                strSql.AppendLine(",UpFunction = @UpFunction");
                strSql.AppendLine(",Uper = @Uper");
                strSql.AppendLine(",DownFunction = @DownFunction");
                strSql.AppendLine(",Downer = @Downer");
                strSql.AppendLine(",MaxNum = @MaxNum");
                strSql.AppendLine(",DownloadCapacity = @DownloadCapacity");
                strSql.AppendLine(",Memo = @Memo");
                strSql.AppendLine(",Status = @Status");
                strSql.AppendLine(",ThreadNum = @ThreadNum");
                strSql.AppendLine(",blnStep = @blnStep");
                strSql.AppendLine(",Class = @Class");
                strSql.AppendLine(",DataClass = @DataClass");
                strSql.AppendLine("Where Code = @Code");

                strSql.AppendLine(";");

                strSql.AppendLine("Delete from TIntfURL Where ParentCode = @Code");
                strSql.AppendLine(";");
                strSql.AppendLine("Delete from TRule Where ParentCode = @Code");
                strSql.AppendLine(";");

                //上传URL
                if (comSend.SelectedValue.ToString() == "DMS")
                {
                    strSql.AppendLine("Insert into TIntfURL(ParentCode,SysCode,blnUp)");
                    strSql.AppendLine("Values ( 'DMS','DMS',1 )");
                }
                else
                {
                    strSql.AppendLine("Insert into TIntfURL(ParentCode,SysCode,blnUp)");
                    strSql.AppendLine("select @Code,o.SysCode, 1 from TSide t,TSideList o");
                    strSql.AppendLine("where t.ID = o.ParentID and t.Code = @Uper");
                }

                //下发URL
                if (comReceive.SelectedValue.ToString() == "DMS")
                {
                    strSql.AppendLine("Insert into TIntfURL(ParentCode,SysCode,blnUp)");
                    strSql.AppendLine("Values ( 'DMS','DMS',0 )");
                }
                else
                {
                    strSql.AppendLine("Insert into TIntfURL(ParentCode,SysCode,blnUp)");
                    strSql.AppendLine("select @Code,o.SysCode, 0 from TSide t,TSideList o");
                    strSql.AppendLine("where t.ID = o.ParentID and t.Code = @Downer");
                }
                //更新TIntf，TIntfURL
                classDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, sqlParameters);

                string sql = CreateTRuleSql();
                DataRow[] drRows = dtRule.Select("blnUp = 1");

                //新增上传规则
                long ID = classDB.ExecuteID(sql, conn, sqlTransaction, this.CreateTRuleParameters(drUpRule));

                foreach (DataRow item in dtUpRulelist.Rows)
                {
                    item["ParentID"] = ID;
                }

                //新增下发规则
                if (radDelay.Checked)
                {
                    ID = classDB.ExecuteID(sql, conn, sqlTransaction, this.CreateTRuleParameters(drDownRule));

                    DataRow drRow = dtDownRulelist.NewRow();
                    foreach (DataRow item in dtDownRulelist.Rows)
                    {
                        item["ParentID"] = ID;
                    }
                    dtUpRulelist.Merge(dtDownRulelist);
                }

                classDB.ExecuteBulkInsert("TRuleList", dtUpRulelist, conn, sqlTransaction);

                sqlTransaction.Commit();

                MessageBox.Show("保存成功！");

            }
            catch (Exception ex)
            {
                sqlTransaction.Rollback();

                MessageBox.Show(ex.Message);

                return;
            }

            conn.Close();
            conn.Dispose();
        }

        private int ExcueNew() {
            int ir = 0;
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("Insert into TIntf(Code,Name,BusinessCode,UpFunction,Uper,DownFunction,Downer");
            //Sequential 20161219 发布环境类型有bit改为Int
            strSql.AppendLine(",MaxNum,DownloadCapacity,Sequential,Memo,Status,ThreadNum,blnStep,Class,DataClass) ");
            strSql.AppendLine(" Values (");
            strSql.AppendLine("@Code,@Name,@BusinessCode,@UpFunction,@Uper,@DownFunction,@Downer");
            strSql.AppendLine(",@MaxNum,@DownloadCapacity,1,@Memo,@Status,@ThreadNum,@blnStep,@Class,@DataClass");
            strSql.AppendLine(" )");
            strSql.AppendLine(";");
            //上传URL
            if (comSend.SelectedValue.ToString() == "DMS")
            {
                strSql.AppendLine("Insert into TIntfURL(ParentCode,SysCode,blnUp)");
                strSql.AppendLine("Values ( 'DMS','DMS',1 )");
            }
            else
            {
                strSql.AppendLine("Insert into TIntfURL(ParentCode,SysCode,blnUp)");
                strSql.AppendLine("select @Code,o.SysCode, 1 from TSide t,TSideList o");
                strSql.AppendLine("where t.ID = o.ParentID and t.Code = @Uper");
            }
            //下发URL
            if (comReceive.SelectedValue.ToString() == "DMS")
            {
                strSql.AppendLine("Insert into TIntfURL(ParentCode,SysCode,blnUp)");
                strSql.AppendLine("Values ( 'DMS','DMS',0 )");
            }
            else
            {
                strSql.AppendLine("Insert into TIntfURL(ParentCode,SysCode,blnUp)");
                strSql.AppendLine("select @Code,o.SysCode, 0 from TSide t,TSideList o");
                strSql.AppendLine("where t.ID = o.ParentID and t.Code = @Downer");
            }

            SqlParameter[] sqlParameters = this.CreateTIntfParameters();

            ClassDB classDB = new ClassDB();
            SqlConnection conn = classDB.CreateConnection();
            conn.Open();
            SqlTransaction sqlTransaction = conn.BeginTransaction();
            try
            {
                //新增TIntf，TIntfURL
                classDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, sqlParameters);

                string sql = CreateTRuleSql();
                //DataRow[] drRows = dtRule.Select("blnUp = 1");

                //解决发送规则和接收规则没有配置报错问题
                if(string.IsNullOrEmpty(drUpRule["Kind"].ToString())){

                    MessageBox.Show("请设置发送规则！");

                    sqlTransaction.Rollback();
                    ir = 1;
                    return ir;
                }
                

                //新增上传规则
                long ID = classDB.ExecuteID(sql, conn, sqlTransaction, this.CreateTRuleParameters(drUpRule));

                foreach (DataRow item in dtUpRulelist.Rows)
                {
                    item["ParentID"] = ID;
                }

                //新增下发规则
                if (radDelay.Checked)
                {

                    //解决发送规则和接收规则没有配置报错问题
                    if(string.IsNullOrEmpty(drDownRule["Kind"].ToString())) {

                        MessageBox.Show("请设置接收规则！");

                        sqlTransaction.Rollback();
                        ir = 1;
                        return ir;
                    }

                    //drRows = dtRule.Select("blnUp = 0");
                    ID = classDB.ExecuteID(sql, conn, sqlTransaction, this.CreateTRuleParameters(drDownRule));

                    DataRow drRow = dtDownRulelist.NewRow();
                    foreach (DataRow item in dtDownRulelist.Rows)
                    {
                        item["ParentID"] = ID;
                    }
                    dtUpRulelist.Merge(dtDownRulelist);
                }

                classDB.ExecuteBulkInsert("TRuleList", dtUpRulelist, conn, sqlTransaction);

                sqlTransaction.Commit();

                MessageBox.Show("保存成功！");

            }
            catch (Exception ex)
            {
                sqlTransaction.Rollback();

                MessageBox.Show(ex.Message);

                return ir;
            }

            conn.Close();
            conn.Dispose();
            return ir;
     
            
        }

        private string CreateTRuleSql()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("Insert into TRule(ParentCode,Kind,blnUp,Frequency,ExecutionTime,WeekDay,MonthDay)");
            strSql.AppendLine(" Values (");
            strSql.AppendLine("@ParentCode,@Kind,@blnUp,@Frequency,@ExecutionTime,@WeekDay,@MonthDay");
            strSql.AppendLine(" )");

            return strSql.ToString();
        }

        private SqlParameter[] CreateTRuleParameters(DataRow drRule)
        {

            
            SqlParameter sqlParCode = new SqlParameter("ParentCode", SqlDbType.NVarChar);
            sqlParCode.Value = this.txtCode.Text.Trim();

            SqlParameter sqlParKind = new SqlParameter("Kind", SqlDbType.SmallInt);
            sqlParKind.Value = drRule["Kind"];

            SqlParameter sqlParblnUp = new SqlParameter("blnUp", SqlDbType.SmallInt);
            sqlParblnUp.Value = drRule["blnUp"];
            
            //SqlParameter sqlParblnTiming = new SqlParameter("blnTiming", SqlDbType.SmallInt);
            //sqlParblnTiming.Value = drRule["blnTiming"];

            SqlParameter sqlParFrequency = new SqlParameter("Frequency", SqlDbType.Int);
            sqlParFrequency.Value = drRule["Frequency"];

            SqlParameter sqlParExecutionTime = new SqlParameter("ExecutionTime", SqlDbType.DateTime);
            sqlParExecutionTime.Value = drRule["ExecutionTime"];

            SqlParameter sqlParWeekDay = new SqlParameter("WeekDay", SqlDbType.NVarChar);
            sqlParWeekDay.Value = drRule["WeekDay"];

            SqlParameter sqlParMonthDay = new SqlParameter("MonthDay", SqlDbType.NVarChar);
            sqlParMonthDay.Value = drRule["MonthDay"];


            SqlParameter[] sqlParameters = { sqlParCode, sqlParKind, sqlParblnUp, sqlParFrequency, 
                                               sqlParExecutionTime, sqlParWeekDay, sqlParMonthDay };

            return sqlParameters;
        }

        private SqlParameter[] CreateTIntfParameters()
        {
            SqlParameter sqlParCode = new SqlParameter("Code", SqlDbType.NVarChar);
            sqlParCode.Value = this.txtCode.Text.Trim();

            SqlParameter sqlParName = new SqlParameter("Name", SqlDbType.NVarChar);
            sqlParName.Value = this.txtName.Text.Trim();

            SqlParameter sqlParBusinessCode = new SqlParameter("BusinessCode", SqlDbType.NVarChar);
            sqlParBusinessCode.Value = this.comBusiness.SelectedValue;

            SqlParameter sqlParUpFunction = new SqlParameter("UpFunction", SqlDbType.NVarChar);
            sqlParUpFunction.Value = this.txtSendFun.Text.Trim();

            SqlParameter sqlParUper = new SqlParameter("Uper", SqlDbType.NVarChar);
            sqlParUper.Value = this.comSend.SelectedValue;

            SqlParameter sqlParDownFunction = new SqlParameter("DownFunction", SqlDbType.NVarChar);
            sqlParDownFunction.Value = this.txtReceiveFun.Text.Trim();

            SqlParameter sqlParDowner = new SqlParameter("Downer", SqlDbType.NVarChar);
            sqlParDowner.Value = this.comReceive.SelectedValue;

            SqlParameter sqlParMaxNum = new SqlParameter("MaxNum", SqlDbType.Int);
            sqlParMaxNum.Value = this.txtNum.Text.Trim();

            SqlParameter sqlParDownNum = new SqlParameter("DownloadCapacity", SqlDbType.Int);
            sqlParDownNum.Value = this.txtDownNum.Text.Trim();

            SqlParameter sqlParMemo = new SqlParameter("Memo", SqlDbType.NVarChar);
            sqlParMemo.Value = this.txtMemo.Text.Trim();

            SqlParameter sqlParStatus = new SqlParameter("Status", SqlDbType.SmallInt);
            if (radStatr1.Checked)
            {
                sqlParStatus.Value = 1;
            }
            else
            {
                sqlParStatus.Value = 0;
            }

            SqlParameter sqlParThreadNum = new SqlParameter("ThreadNum", SqlDbType.Int);
            sqlParThreadNum.Value = this.txtThreadNum.Text.Trim();

            SqlParameter sqlParblnStep = new SqlParameter("blnStep", SqlDbType.SmallInt);
            if (radDelay.Checked)
            {
                sqlParblnStep.Value = 1;
            }
            else
            {
                sqlParblnStep.Value = 0;
            }


            SqlParameter sqlParClass = new SqlParameter("Class", SqlDbType.SmallInt);
            if (radClass1.Checked)
            {
                sqlParClass.Value = 1;
            }
            else
            {
                sqlParClass.Value = 0;
            }

            SqlParameter sqlParDataClass = new SqlParameter("DataClass", SqlDbType.SmallInt);
            sqlParDataClass.Value = this.comDataClass.SelectedIndex;

            SqlParameter sqlParParentID = new SqlParameter("ParentID", SqlDbType.Int);
            if (!String.IsNullOrEmpty(this.drUpRule["ID"].ToString()))
            {
                sqlParParentID.Value = Convert.ToInt64(this.drUpRule["ID"]);
            }
            else
            {
                sqlParParentID.Value = 0;
            }


            SqlParameter[] sqlParameters = {sqlParCode,sqlParName,sqlParBusinessCode,sqlParUpFunction,sqlParUper,sqlParDownFunction,sqlParDowner,
                                           sqlParMaxNum,sqlParDownNum,sqlParMemo,sqlParStatus,sqlParThreadNum,sqlParblnStep,sqlParClass,sqlParDataClass,sqlParParentID};

            return sqlParameters;
        }

        private DataTable SearchRule(string ParentCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("Select t.* from TRule t");
            strSql.AppendLine("Where t.ParentCode = '" + ParentCode + "'");

            DataTable dtData = new DataTable();
            ClassDB classDB = new ClassDB();
            dtData = classDB.ExecuteDataTable(strSql.ToString());

            return dtData;
        }

        private void radOn_CheckedChanged(object sender, EventArgs e)
        {
            if (radOn.Checked)
            {
                this.button1.Enabled = false;
            }
            else
            {
                this.button1.Enabled = true;
            }
        }

        private DataTable SearchRuleList(string strParentID)
        {
            StringBuilder strSql = new StringBuilder();
            if (strParentID.Equals(string.Empty))
            {
                strSql.AppendLine("select * from TRuleList t where t.ParentID = 0");
            }
            else
            {
                strSql.AppendLine("select * from TRuleList t where t.ParentID = " + strParentID);
            }
            ClassDB classDB = new ClassDB();
            DataTable dtData = classDB.ExecuteDataTable(strSql.ToString());

            return dtData;
        }

        private void CacuStartTime(string kind, DataRow drRow, DataTable dtStartTime)
        {
            //intfsetting intfsetting = new intfsetting();
            DateTime ExecutionTime;
            ClassDB classDB = new ClassDB();
            DataTable dtNow = classDB.ExecuteDataTable("select getdate() as DBDateTime");
            DateTime dateTime = Convert.ToDateTime(dtNow.Rows[0]["DBDateTime"]);

            ClassIntf classIntf = new ClassIntf();
            ExecutionTime = classIntf.CalculateExecutionTime(drRow, dtStartTime, dateTime);
            if (kind.Equals("1"))
            {

                //if (drRow["blnTiming"].ToString().Equals("1"))
                //{
                //    ExecutionTime = dateTime.AddMinutes(Convert.ToDouble(drRow["Frequency"]));

                //}
                //else
                //{
                //    ExecutionTime = intfsetting.CalculateExecutionTime(drRow, dtStartTime);
                //}

                drRow["ExecutionTime"] = ExecutionTime;


            }
            else if (kind.Equals("2"))
            {
                string strWeekDay = string.Empty;
                strWeekDay = drRow["WeekDay"].ToString();
                //ExecutionTime = intfsetting.CalculateExecutionTime(drRow, dtStartTime);

                drRow["ExecutionTime"] = ExecutionTime;


            }
            else if (kind.Equals("3"))
            {
                string strMonthDay = string.Empty;
                strMonthDay = drRow["MonthDay"].ToString();
                //ExecutionTime = intfsetting.CalculateExecutionTime(drRow, dtStartTime);

                drRow["ExecutionTime"] = ExecutionTime;

            }
            else if (kind.Equals("4"))
            {
                //ExecutionTime = intfsetting.CalculateExecutionTime(drRow, dtStartTime);

                drRow["ExecutionTime"] = ExecutionTime;

            }
        }

        private void radClass1_CheckedChanged(object sender, EventArgs e)
        {

            InitializeSideCon();

        }

        private void comDataClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comDataClass.SelectedIndex == 0 && this.radClass0.Checked)
            {
                this.radDelay.Checked = true;
                this.radOn.Enabled = false;
            }
            else
            {
                this.radOn.Enabled = true;
            }
        }


    }
}
